package com.note.simpleadmin.module.advert.service;

import com.note.simpleadmin.entity.TbBanner;
import com.note.simpleadmin.enu.DeleteEnum;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.advert.dao.BannerRepository;
import com.note.simpleadmin.utils.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 *@ClaseName:BannerService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/29 0029 11:05
 */
@Service
public class BannerService {
    @Autowired
    private BannerRepository bannerRepository;
    public List<TbBanner> findAll()
    {
        return bannerRepository.findByIsDelete(DeleteEnum.NORMAL.getValue());
    }
    public  void save(TbBanner banner){
        banner.setId(IdGenerator.id());
        banner.setIsDelete(DeleteEnum.NORMAL.getValue());
        bannerRepository.save(banner);
    }
    public  void update(TbBanner banner){
        banner.setIsDelete(DeleteEnum.NORMAL.getValue());
        bannerRepository.save(banner);
    }
    public  void delete(String id) throws  DataNotExistException{
        TbBanner banner=getOne(id);
        if(banner==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        banner.setIsDelete(DeleteEnum.DELETE.getValue());
        bannerRepository.save(banner);
    }
    public TbBanner getOne(String id)
    {
        List<TbBanner> list=bannerRepository.findByIdAndIsDelete(id,DeleteEnum.NORMAL.getValue());
        if(list.size()>0)
        {
            return list.get(0);
        }
        return null;
    }
}
